package com.hellobike.haxing.modules.operate.service.impl;

import com.alibaba.excel.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hellobike.haxing.common.constant.CacheConstant;
import com.hellobike.haxing.common.util.RedisUtil;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.operate.entity.OperateAdvert;
import com.hellobike.haxing.modules.operate.mapper.OperateAdvertMapper;
import com.hellobike.haxing.modules.operate.service.IOperateAdvertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: advert广告弹框
 * @Author: haxing-boot
 * @Date: 2020-01-08
 * @Version: V1.0
 */
@Service
public class OperateAdvertServiceImpl extends ServiceImpl<OperateAdvertMapper, OperateAdvert> implements IOperateAdvertService {
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Result<?> updateAdvert(OperateAdvert operateAdvert) {
        // 1<=beginTime < 3
        QueryWrapper<OperateAdvert> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().le(OperateAdvert::getBeginTime, operateAdvert.getBeginTime()).gt(OperateAdvert::getEndTime, operateAdvert.getBeginTime()).notIn(OperateAdvert::getId, operateAdvert.getId());
        int count = super.count(queryWrapper);
        if (count > 0) {
            return Result.error("编辑失败;开始时间与数据库已存在时间重叠!");
        }
        // 1<=endTime < 3
        QueryWrapper<OperateAdvert> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.lambda().le(OperateAdvert::getBeginTime, operateAdvert.getEndTime()).gt(OperateAdvert::getEndTime, operateAdvert.getEndTime()).notIn(OperateAdvert::getId, operateAdvert.getId());
        int count2 = super.count(queryWrapper);
        if (count2 > 0) {
            return Result.error("编辑失败;结束时间与数据库已存在时间重叠!");
        }
        // 1<=statrTime && endTime < 3
        QueryWrapper<OperateAdvert> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.lambda().le(OperateAdvert::getBeginTime, operateAdvert.getBeginTime()).gt(OperateAdvert::getEndTime, operateAdvert.getEndTime()).notIn(OperateAdvert::getId, operateAdvert.getId());
        int count3 = super.count(queryWrapper);
        if (count3 > 0) {
            return Result.error("编辑失败;开始时间和结束时间与数据库已存在时间重叠!");
        }
        baseMapper.updateById(operateAdvert);
        redisUtil.del(CacheConstant.OPERATE_ADVERT_LIST);
        redisUtil.del(CacheConstant.OPERATE_ADVERT_TYPE + operateAdvert.getLinkType().toString());
        return Result.ok("编辑成功!");
    }

    @Override
    public Result<?> saveAdvert(OperateAdvert operateAdvert) {
        // 1<=beginTime < 3
        QueryWrapper<OperateAdvert> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().le(OperateAdvert::getBeginTime, operateAdvert.getBeginTime()).gt(OperateAdvert::getEndTime, operateAdvert.getBeginTime());
        List<OperateAdvert> advertList = super.list(queryWrapper);
        if (!CollectionUtils.isEmpty(advertList)) {
            for (OperateAdvert advert : advertList) {
                if (advert.getUserType() == 1) {
                    return Result.error("添加失败;开始时间与数据库已存在时间重叠!");
                } else if (advert.getUserType() == operateAdvert.getUserType()) {
                    return Result.error("添加失败;开始时间与数据库已存在时间重叠!");
                }
            }
        }
        // 1<=endTime < 3
        QueryWrapper<OperateAdvert> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.lambda().le(OperateAdvert::getBeginTime, operateAdvert.getEndTime()).gt(OperateAdvert::getEndTime, operateAdvert.getEndTime());
        advertList = super.list(queryWrapper2);
        if (!CollectionUtils.isEmpty(advertList)) {
            for (OperateAdvert advert : advertList) {
                if (advert.getUserType() == 1) {
                    return Result.error("添加失败;结束时间与数据库已存在时间重叠!");
                } else if (advert.getUserType() == operateAdvert.getUserType()) {
                    return Result.error("添加失败;结束时间与数据库已存在时间重叠!");
                }
            }
        }
        // 1<=statrTime && endTime < 3
        QueryWrapper<OperateAdvert> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.lambda().le(OperateAdvert::getBeginTime, operateAdvert.getBeginTime()).gt(OperateAdvert::getEndTime, operateAdvert.getEndTime());
        advertList = super.list(queryWrapper3);
        if (!CollectionUtils.isEmpty(advertList)) {
            for (OperateAdvert advert : advertList) {
                if (advert.getUserType() == 1) {
                    return Result.error("添加失败;开始时间和结束时间与数据库已存在时间重叠!");
                } else if (advert.getUserType() == operateAdvert.getUserType()) {
                    return Result.error("添加失败;开始时间和结束时间与数据库已存在时间重叠!");
                }
            }
        }
        baseMapper.insert(operateAdvert);
        redisUtil.del(CacheConstant.OPERATE_ADVERT_LIST);
        redisUtil.del(CacheConstant.OPERATE_ADVERT_TYPE + operateAdvert.getLinkType().toString());
        return Result.ok("添加成功！");
    }
}
